package com.kaba.xtzhgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaba.bean.*;
import com.kaba.util.JwtUtil;
import com.kaba.xtzhgl.service.SysRoleService;
import com.kaba.xtzhgl.service.SysUserService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lvmengjie
 * @Date 2021/3/4
 * @Description TODO
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Slf4j
public class SysUserContorller {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;


    @GetMapping("/queryUserName")
    public ResponseResult queryUserName(SysUser sysUser){
        List<SysUser> list =sysUserService.queryUserName(sysUser);

        return ResponseResult.success(ResultCode.SUCCESS,list);
    }

    @PostMapping("/logout")
    public ResponseResult logout(){
        String data="logoutsuccess";
        return ResponseResult.success(data);


    }


    @PostMapping("/login")
    public ResponseResult login(@RequestBody  SysUser sysUser) {
        List<SysUser> list=sysUserService.queryUserName(sysUser);
        System.out.println(list);

        if (!list.isEmpty()) {
            String token=JwtUtil.generateToken(sysUser.getUserName(),sysUser.getPassword());
            List<SysUser> aaa=sysUserService.queryUserName(sysUser);
            System.out.println(token);
            Map<Object,Object> map=new HashMap<>();
            map.put("list",aaa);
            map.put("token",token);
            return ResponseResult.success(ResultCode.SUCCESS,map);
        }else{
            return ResponseResult.error(ResultCode.USER_CREDENTIALS_ERROR);
        }
    }


    @GetMapping("/info")
    public ResponseResult info(@RequestParam("token") String token){
        //验证token的合法和有效性
        Claims tokenvalue=JwtUtil.verifyJwt(token);
        if (!tokenvalue.equals("")){
            //如果ok返回用户信息
            String username = (String) tokenvalue.get("username");
            System.out.println(tokenvalue);
            System.out.println(username);
            SysUser sysUser=new SysUser();
            sysUser.setUserName(username);
            //查出用户信息并返回
            List<SysUser> list=sysUserService.queryUserName(sysUser);
            //查出相应角色并返回
            List<SysRole> rolelist=sysRoleService.querysByname(username);
            System.out.println(list);
            VoUserInfo vo=new VoUserInfo();
            vo.setAvatar(list.get(0).getAvatar());
            vo.setIntroduction(list.get(0).getNickType());
            vo.setUsers(list);
            vo.setRoles(rolelist);
            vo.setName(list.get(0).getUserName());

            return ResponseResult.success(ResultCode.SUCCESS,vo);

        }else {
            //否则就500
            return ResponseResult.error(ResultCode.LECTURER_REQUISITION_REGISTERED);


        }


    }

}
